package com.gopay.checkorder.service.impl.tpas;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.checkorder.service.CheckTpasOrderService;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.constants.bank.tpas.TpasOrderStatus;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasOrder;
import com.gopay.common.domain.bank.tpas.TpasResult;
import com.gopay.common.domain.bank.tpas.mwa.OrderCards;
import com.gopay.common.domain.bank.tpas.mwa.OrderMessage;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.eis.ofpay.TpasChargeInteractRemoteService;

@Service("OFPAY-TPAS")
public class CheckOrderOfpayService implements CheckTpasOrderService{
	private Logger logger = Logger.getLogger("OFPAY");
	
	@Resource(name = "tpasChargeInteractRemoteService")
	private TpasChargeInteractRemoteService mobileTrafficRechargeRemoteService;
	
	@Override
	public CheckOrderResult checkOrder(TpasOrder order, String autoCheckSuccess) {
		TpasMessage msg = new TpasMessage();
		msg.setBankOrderId(order.getOutOrderId());
		msg.setOrderId(order.getOrderId());
		msg.setTransType(order.getProdCode());
		CheckOrderResult result = new CheckOrderResult();
		InAccInfo accInfo = new InAccInfo();
		accInfo.setOrderId(order.getOrderId());
		accInfo.setCardType(order.getProdCode());
		result.setAccInfo(accInfo);
		try {
			if(StringUtils.equals(TpasOrderStatus.PROCESS, order.getOrderStatus()) && IntTxnCd._01618.value.equals(order.getProdCode())){//add by zhoupeng 20170622
                result.setSuccess(true);
            	TpasMessage base = new TpasMessage();
            	base.setOrderId(order.getOrderId());
            	TpasResult status01618 = mobileTrafficRechargeRemoteService.queryOrderInfo(base);
            	logger.info("视频会员卡进行中的数据调用OF查询结果result="+result);
            	if(status01618.isSuccess() && status01618.getDefaultModel() != null){
            		OrderMessage orderMessage = (OrderMessage)status01618.getDefaultModel();
            		if (TpasMwaChargeConstants.CHARGE_SUCCESS.equals(orderMessage.getRetCode())) {
            			result.setBankFailure(false);// 下面转成true了
            			List<OrderCards> cardsList = orderMessage.getCardsList();// 卡密
            			if (cardsList != null && cardsList.size() == 1) {
            				for (OrderCards orderCards : cardsList) {
            					String cardPws = orderCards.getCardPws();
            					result.setCardPws(cardPws);
            				}
            			}
            		} else if (TpasMwaChargeConstants.CHARGE_FAIL.equals(orderMessage.getRetCode())) {
            			result.setBankFailure(true);
            		}else{
            			result.setSuccess(false);	//如果是进行中 直接给false 等待下一个5分钟再查
            		}
            	}else{
					result.setSuccess(false);
				}
            }else{
				TpasResult status = mobileTrafficRechargeRemoteService.queryOrderStatus(msg);
				String model = (String) status.getDefaultModel();
				if(status.isSuccess()){
					result.setSuccess(true);
					if(StringUtils.equals(model, TpasMwaChargeConstants.CHARGE_SUCCESS)){
						result.setBankFailure(false);
					}else if(StringUtils.equals(model, TpasMwaChargeConstants.CHARGE_FAIL)){
						result.setBankFailure(true);
					}else{
						result.setSuccess(false);	//进行中
					}
				}else{
					result.setSuccess(false);
				}
            }
		} catch (GopayException e) {
			logger.error("checkorder error!! orderid:"+order.getOrderId(), e);
			result.setSuccess(false);
		}
		return result;
	}

}
